package com.cd.javacxy.module.web.service.impl;

import com.cd.javacxy.common.model.entity.CxyShuoshuo;
import com.cd.javacxy.common.model.entity.CxyShuoshuoComment;
import com.cd.javacxy.common.model.entity.CxyShuoshuoLike;
import com.cd.javacxy.common.model.enums.*;
import com.cd.javacxy.common.model.query.MessageParams;
import com.cd.javacxy.common.model.query.ShuoShuoQuery;
import com.cd.javacxy.common.utils.CxyUtils;
import com.cd.javacxy.common.utils.ImageUtils;
import com.cd.javacxy.common.utils.Page;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.web.dao.CxyShuoshuoDao;
import com.cd.javacxy.module.web.dao.CxyShuoshuoLikeDao;
import com.cd.javacxy.module.web.service.CxyMessageService;
import com.cd.javacxy.module.web.service.CxyShuoshuoCommentService;
import com.cd.javacxy.module.web.service.CxyShuoshuoService;
import com.cd.javacxy.module.web.service.CxyUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * CxyShuoshuo的服务接口的实现类
 *
 * @author yanghy
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CxyShuoshuoServiceImpl implements CxyShuoshuoService {

    private final CxyShuoshuoDao cxyShuoshuoDao;

    private final CxyUserService userService;

    private final CxyShuoshuoCommentService cxyShuoshuoCommentService;

    private final CxyMessageService messageService;

    private final FormateAtService formateAtService;

    private final CxyShuoshuoLikeDao cxyShuoshuoLikeDao;

    @Override
    public PageResult<CxyShuoshuo> findShuoShuoList(ShuoShuoQuery shuoShuoQuery) {
        int count = this.cxyShuoshuoDao.selectCount(shuoShuoQuery);
        int size = PageSize.PAGE_SIZE10.getSize();
        int pageNum = 1;
        if (shuoShuoQuery.getPageNum() != 1) {
            pageNum = shuoShuoQuery.getPageNum();
        }
        Page page = new Page(pageNum, count, size);
        shuoShuoQuery.setPage(page);
        List<CxyShuoshuo> shuoShuos = this.cxyShuoshuoDao.selectList(shuoShuoQuery);
        return new PageResult<>(page, shuoShuos);
    }

    @Override
    public CxyShuoshuo findShuoShuo(ShuoShuoQuery shuoShuoQuery) {
        List<CxyShuoshuo> list = this.cxyShuoshuoDao.selectList(shuoShuoQuery);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = BussinessException.class)
    public int addShuoShuoComment(CxyShuoshuoComment shuoShuoComment) throws BussinessException {

        String content = shuoShuoComment.getContent();
        if (StringUtils.isEmpty(content) || content.length() > TextLengthEnum.TEXT.getLength()) {
            throw new BussinessException("参数错误");
        }
        //给网页加链接
        content = CxyUtils.addLink(content);
        Set<Integer> userIdSet = new HashSet<>();
        String formatContent = formateAtService.generateRefererLinks(content, userIdSet);
        //TODO给用户发消息
        shuoShuoComment.setContent(formatContent);
        shuoShuoComment.setCreateTime(LocalDateTime.now());
        this.cxyShuoshuoDao.updateShuoShuoCommentCount(shuoShuoComment.getShuoshuoId());
        this.cxyShuoshuoCommentService.insert(shuoShuoComment);
        this.userService.addMark(MarkEnum.MARK_COMMENT.getMark(), shuoShuoComment.getUserId());

        Integer shuoshuoId = shuoShuoComment.getShuoshuoId();
        MessageParams messageParams = new MessageParams();
        messageParams.setArticleId(shuoshuoId);
        messageParams.setArticleType(ArticleType.SHUOSHUO);
        ShuoShuoQuery shuoShuoQuery = new ShuoShuoQuery();
        shuoShuoQuery.setShuoShuoId(shuoshuoId);
        CxyShuoshuo ss = findShuoShuo(shuoShuoQuery);
        messageParams.setArticleUserId(ss.getUserId());
        messageParams.setMessageType(MessageType.COMMENT_MESSAGE);
        messageParams.setSendUserName(shuoShuoComment.getUserName());
        messageParams.setSendUserId(shuoShuoComment.getUserId());
        userIdSet.add(ss.getUserId());
        messageParams.setReceiveUserIds(userIdSet);
        messageParams.setCommentId(shuoShuoComment.getId());
        messageService.createMessage(messageParams);
        return 0;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = BussinessException.class)
    public int doShuoShuoLike(CxyShuoshuoLike shuoShuoLike) throws BussinessException {
        ShuoShuoQuery shuoShuoQuery = new ShuoShuoQuery();
        shuoShuoQuery.setUserId(shuoShuoLike.getUserId());
        System.out.println(shuoShuoQuery.getShuoShuoId());
        shuoShuoQuery.setShuoShuoId(shuoShuoLike.getShuoshuoId());
        if (this.findShuoShuoLike(shuoShuoQuery).size() >= 1) {
            throw new BussinessException("您已经点过赞了");
        }
        shuoShuoLike.setCreateTime(LocalDateTime.now());
        this.cxyShuoshuoDao.updateShuoShuoLikeCount(shuoShuoLike.getShuoshuoId());
        this.cxyShuoshuoLikeDao.insert(shuoShuoLike);
        return 0;
    }

    public List<CxyShuoshuoLike> findShuoShuoLike(ShuoShuoQuery shuoShuoQuery) {
        List<CxyShuoshuoLike> shuoShuoLikeList = this.cxyShuoshuoLikeDao.selectList(shuoShuoQuery);
        System.out.println(shuoShuoLikeList);
        return shuoShuoLikeList;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = BussinessException.class)
    public int addShuoShuo(CxyShuoshuo shuoShuo) throws BussinessException {
        String content = shuoShuo.getContent();
        if (StringUtils.isEmpty(content) || content.length() > TextLengthEnum.TEXT.getLength()) {
            throw new BussinessException("参数错误");
        }
        //给网页加链接
        content = CxyUtils.addLink(content);
        Set<Integer> userIdSet = new HashSet<>();
        String formatContent = formateAtService.generateRefererLinks(content, userIdSet);
        //TODO给用户发消息
        String thumnail = ImageUtils.createThumbnail(shuoShuo.getImageUrl(), false);
        shuoShuo.setImageUrlSmall(thumnail);
        shuoShuo.setContent(formatContent);
        shuoShuo.setCreateTime(LocalDateTime.now());
        shuoShuo.setLikeCount(0);
        shuoShuo.setCommentCount(0);
        this.cxyShuoshuoDao.insert(shuoShuo);
        this.userService.addMark(MarkEnum.MARK_SHUOSHUO.getMark(), shuoShuo.getUserId());

        MessageParams messageParams = new MessageParams();
        messageParams.setArticleId(shuoShuo.getId());
        messageParams.setArticleType(ArticleType.SHUOSHUO);
        messageParams.setArticleUserId(shuoShuo.getUserId());
        messageParams.setMessageType(MessageType.AT_ARTICLE_MESSAGE);
        messageParams.setSendUserName(shuoShuo.getUserName());
        messageParams.setSendUserId(shuoShuo.getUserId());
        messageParams.setReceiveUserIds(userIdSet);
        messageService.createMessage(messageParams);
        return 0;
    }
}